package drds.binlog.client.impl;

import drds.binlog.client.NodeAccessStrategy;

import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;

/**
 * 简单版本的node访问实现
 */
public class SimpleNodeAccessStrategy implements NodeAccessStrategy
{

    private List<SocketAddress> socketAddressList = new ArrayList<SocketAddress>();
    private int index = 0;

    public SimpleNodeAccessStrategy(List<? extends SocketAddress> socketAddressList)
    {
        if (socketAddressList == null || socketAddressList.size() < 1)
        {
            throw new IllegalArgumentException("at least 1 node required.");
        }
        this.socketAddressList.addAll(socketAddressList);
    }

    public SocketAddress nextNode()
    {
        try
        {
            return socketAddressList.get(index);
        } finally
        {
            index = (index + 1) % socketAddressList.size();
        }
    }

    @Override
    public SocketAddress currentNode()
    {
        return socketAddressList.get(index);
    }

}
